//
//  CQHorizontalImageLabelView.m
//  BiaoliApp
//
//  Created by qian on 2020/12/25.
//

#import "CQHorizontalImageLabelView.h"
#import "CQLeftIconRightCustomConstraintHelper.h"

@interface CQHorizontalImageLabelView () {
    
}
@property (nonatomic, assign, readonly) CGFloat iconHeight;
@property (nonatomic, assign, readonly) CGFloat iconTitleSpacing;
@property (nonatomic, assign, readonly) UIControlContentHorizontalAlignment imageLabelContentHorizontalAlignment;

@end

@implementation CQHorizontalImageLabelView

- (instancetype)initWithIconHeight:(CGFloat)iconHeight
                  iconTitleSpacing:(CGFloat)iconTitleSpacing
        contentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment
{
    
    self = [super initWithFrame:CGRectZero];
    if (self) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor clearColor];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        
        [CQLeftIconRightCustomConstraintHelper addAndConstraintContainer:self
                                                       withLeftImageView:imageView
                                                         rightCustomView:titleLabel
                                                              iconHeight:iconHeight
                                                        iconTitleSpacing:iconTitleSpacing
                                              contentHorizontalAlignment:contentHorizontalAlignment];
        _iconHeight = iconHeight;
        _iconTitleSpacing = iconTitleSpacing;
        _imageLabelContentHorizontalAlignment = contentHorizontalAlignment;
        
        self.imageView = imageView;
        self.titleLable = titleLabel;
    }
    return self;
}

#pragma mark - Update
/// 更新图片，同时会根据图片是否为空，来重新调整约束
- (void)updateAndReconstraintImageViewWithImage:(nullable UIImage *)image {
    self.imageView.image = image;
    
    if (image == nil) {
        [CQLeftIconRightCustomConstraintHelper reConstraintContainer:self
                                                   withLeftImageView:self.imageView
                                                     rightCustomView:self.titleLable
                                                          iconHeight:0
                                                    iconTitleSpacing:0
                                          contentHorizontalAlignment:self.imageLabelContentHorizontalAlignment];
        
    } else {
        [CQLeftIconRightCustomConstraintHelper reConstraintContainer:self
                                                   withLeftImageView:self.imageView
                                                     rightCustomView:self.titleLable
                                                          iconHeight:self.iconHeight
                                                    iconTitleSpacing:self.iconTitleSpacing
                                          contentHorizontalAlignment:self.imageLabelContentHorizontalAlignment];
    }
}



- (void)addTarget:(nullable id)target action:(SEL)action {
//    [target performSelector:action target:target argument:nil order:0 modes:@[NSDefaultRunLoopMode]];
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tapGR];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
